package com.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.bean.SearchHotKeyBean;
import com.ddd.wandemo.R;
import com.gcssloop.widget.PagerGridLayoutManager;
import com.gcssloop.widget.PagerGridSnapHelper;
import com.ui.DaySelectAdapter;
import com.util.DensityUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class DaySelectTabLayout extends FrameLayout implements PagerGridLayoutManager
        .PageListener, DaySelectAdapter.ItemPositionListener {

    private int mRows = 1;
    private int mColumns = 7;
    private PagerGridLayoutManager mLayoutManager;
    private RecyclerView mRecyclerView;
    private DaySelectAdapter mAdapter;
    private LinearLayout mLlIndicate;
    private Context mContext;
    private int curIndex = 0;
    private int oldIndex;
    private SelectedListener mListener;
    private int pageIndex;

    public DaySelectTabLayout(Context context) {
        this(context, null);
    }

    public DaySelectTabLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DaySelectTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView(context, attrs, defStyleAttr);
    }

    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        View selectView = LayoutInflater.from(context).inflate(R.layout.view_select_day, this, true);
        //        LinearLayout llContain = selectView.findViewById(R.id.ll_contain);
        //        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        //        addView(llContain, params);


        mLlIndicate = selectView.findViewById(R.id.ll_indicate);

        mRecyclerView = selectView.findViewById(R.id.rv);
        mLayoutManager = new PagerGridLayoutManager(mRows, mColumns, PagerGridLayoutManager.HORIZONTAL);
        // 水平分页布局管理器
        mLayoutManager.setPageListener(this);    // 设置页面变化监听器
        mRecyclerView.setLayoutManager(mLayoutManager);

        // 设置滚动辅助工具
        PagerGridSnapHelper pageSnapHelper = new PagerGridSnapHelper();
        pageSnapHelper.attachToRecyclerView(mRecyclerView);

//        PagerConfig.setShowLog(true);

        mAdapter = new DaySelectAdapter(context);
        mAdapter.setOnItemPositionListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    public void setData(List<SearchHotKeyBean> data) {
        mAdapter.setData(data);
    }

    @Override
    public void onItemPositionListener(int position) {
        mListener.selectedPosition(position);
        int a = position / mColumns;
        if (a != pageIndex) {
            mLayoutManager.smoothScrollToPage(a);
        }
    }

    @Override
    public void onPageSizeChanged(int pageSize) {
        initIndicate(pageSize);
    }

    private void initIndicate(int pageSize) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DensityUtils.dp2px(mContext, 5), DensityUtils.dp2px(mContext, 5));
        params.rightMargin = DensityUtils.dp2px(mContext, 8);
        for (int i = 0; i < pageSize; i++) {
            if (i == 1) {
                params.leftMargin = DensityUtils.dp2px(mContext, 8);
            }
            CircleImageView imageView = new CircleImageView(mContext);
            imageView.setImageDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.notification_icon_bg_color)));
            mLlIndicate.addView(imageView, params);
        }
    }

    @Override
    public void onPageSelect(int pageIndex) {
        oldIndex = curIndex;
        curIndex = pageIndex;

        indicateUnSelect(oldIndex);
        indicateSelect(curIndex);

        this.pageIndex = pageIndex;
    }

    public void changeSelected(int index) {
        mAdapter.changePage(index);
    }

    public void indicateSelect(int index) {
        ((CircleImageView) mLlIndicate.getChildAt(index)).setImageDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.colorAccent)));
    }

    public void indicateUnSelect(int index) {
        ((CircleImageView) mLlIndicate.getChildAt(index)).setImageDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.notification_icon_bg_color)));
    }

    public interface SelectedListener {
        void selectedPosition(int position);
    }

    public void setSelectedListener(SelectedListener listener) {
        this.mListener = listener;
    }
}
